package com.javafactroy.kunlong.framework.batch;

import com.javafactroy.kunlong.base.metadata.model.TMetadataModel;
import com.javafactroy.kunlong.common.utils.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

@Configuration
@EnableBatchProcessing
@Slf4j
@ConditionalOnProperty(prefix = "spring.datasource.oracle", name = "enabled", havingValue = "true")
public class SimpleJobConfig {
    @Autowired
    TMetadataModel metadataModel;

//    @Autowired
//    @Qualifier("oracleDataSource")
//    DataSource dataSource;

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;


    JobRepository jobRepository(){
        return  null;
    }

    @Bean
    public Step simpleStep() {
        return stepBuilderFactory.get("simpleStep")
                .tasklet((contribution, chunkContext) -> {
                    System.out.println("Hello, World!");
                    List list=metadataModel.selectMysqlColumns("employee");
                    log.info("list={}", CommonUtils.toJSONString(list));
                    return RepeatStatus.FINISHED;
                })
                .build();
    }

    @Bean
    public Job simpleJob() {
        return jobBuilderFactory.get("simpleJob")
                .start(simpleStep())
                .build();
    }

}